package com.studyweb.controller;

import com.github.pagehelper.PageInfo;
import com.studyweb.pojo.entity.PaperScore;
import com.studyweb.service.PaperScoreService;
import com.studyweb.pojo.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/paper-score")
@CrossOrigin
public class PaperScoreController {
    
    @Autowired
    private PaperScoreService paperScoreService;
    
    /**
     * 分页查询试卷分值
     * @param page 页码
     * @param size 每页大小
     * @param examName 考试名称（模糊查询）
     * @param subject 科目（模糊查询）
     * @return 分页结果
     */
    @GetMapping("/list")
    public Result<PageInfo<PaperScore>> list(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String examName,
            @RequestParam(required = false) String subject) {
        try {
            PageInfo<PaperScore> pageInfo = paperScoreService.getPage(page, size, examName, subject);
            return Result.success(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("查询失败");
        }
    }
    
    /**
     * 根据ID查询试卷分值
     * @param id 试卷分值ID
     * @return 试卷分值对象
     */
    @GetMapping("/{id}")
    public Result<PaperScore> getById(@PathVariable Long id) {
        try {
            PaperScore paperScore = paperScoreService.getById(id);
            if (paperScore != null) {
                return Result.success(paperScore);
            } else {
                return Result.error("未找到对应数据");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("查询失败");
        }
    }
    
    /**
     * 新增试卷分值
     * @param paperScore 试卷分值对象
     * @return 是否成功
     */
    @PostMapping
    public Result<Boolean> save(@RequestBody PaperScore paperScore) {
        try {
            boolean result = paperScoreService.save(paperScore);
            if (result) {
                return Result.success(true);
            } else {
                return Result.error("保存失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("保存失败");
        }
    }
    
    /**
     * 更新试卷分值
     * @param paperScore 试卷分值对象
     * @return 是否成功
     */
    @PutMapping
    public Result<Boolean> update(@RequestBody PaperScore paperScore) {
        try {
            boolean result = paperScoreService.update(paperScore);
            if (result) {
                return Result.success(true);
            } else {
                return Result.error("更新失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("更新失败");
        }
    }
    
    /**
     * 删除试卷分值
     * @param id 试卷分值ID
     * @return 是否成功
     */
    @DeleteMapping("/{id}")
    public Result<Boolean> delete(@PathVariable Long id) {
        try {
            boolean result = paperScoreService.delete(id);
            if (result) {
                return Result.success(true);
            } else {
                return Result.error("删除失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("删除失败");
        }
    }
}